/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.BaoCaoTonDAO;
import dao.ChiTietPhieuNhapDAO;
import dao.CuaHangDAO;
import dao.HangSanXuatDAO;
import dao.PhieuNhapDAO;
import dao.QuiDinhDAO;
import dao.SanPhamDAO;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.BaoCaoTon;
import pojo.ChiTietPhieuNhapId;
import pojo.CuaHang;
import pojo.HangSanXuat;
import pojo.NguoiDung;
import pojo.PhieuNhap;
import pojo.QuyDinh;
import pojo.SanPham;

/**
 *
 * @author LaiHuynh
 */
public class QuanLySanPham extends javax.swing.JInternalFrame {

    private DefaultTableModel dtm;
    private DefaultComboBoxModel dcbm;
    private DefaultComboBoxModel dcbm1;
    private NguoiDung user;
    /**
     * Creates new form QuanLySanPham
     */
    public QuanLySanPham() {
        initComponents();
        this.dcbm = new DefaultComboBoxModel();
        this.jcbHangSX.setModel(dcbm);
        this.dcbm1 = new DefaultComboBoxModel();
        this.jcbCuaHang.setModel(dcbm1);
    }
    public QuanLySanPham(NguoiDung nguoidung) {
        initComponents();
        this.dcbm = new DefaultComboBoxModel();
        this.jcbHangSX.setModel(dcbm);
        this.dcbm1 = new DefaultComboBoxModel();
        this.jcbCuaHang.setModel(dcbm1);
        this.user=nguoidung;
        //JOptionPane.showMessageDialog(this, user.getTenNguoiDung());
    }

    private void hienThiDanhSach() {
        //doc du lieu
        this.dtm = new DefaultTableModel();
        List<SanPham> ds = SanPhamDAO.layDanhSachSanPham();
        this.dtm.addColumn("Ma San Pham");
        this.dtm.addColumn("Ten San Pham");
        this.dtm.addColumn("Gia Ban");
        this.dtm.addColumn("So Luong Ton");
        this.dtm.addColumn("Hang SX");
        //this.dtm.addColumn("Cua Hang");
        for (int i = 0; i < ds.size(); i++) {
            SanPham sp = ds.get(i);
            Vector vt1 = new Vector();
            vt1.add(sp.getMaSanPham());
            vt1.add(sp.getTenSanPham());
            vt1.add(sp.getGiaBan());
            vt1.add(sp.getSoLuongTon());
            HangSanXuat hsx = sp.getHangSanXuat();
            CuaHang ch = sp.getCuaHang();
            vt1.add(hsx.getTenHang());
            //vt1.add(ch.getMaCuaHang());
            this.dtm.addRow(vt1);
        }
        this.jtDanhSach.setModel(dtm);
    }
    private void hienthidanhsachtheoch(){
        this.dtm=new DefaultTableModel();
        CuaHang ch=(CuaHang) this.jcbCuaHang.getSelectedItem();
        List<SanPham> ds=SanPhamDAO.laysptheoch(ch.getMaCuaHang());
        this.dtm.addColumn("Ma San Pham");
        this.dtm.addColumn("Ten San Pham");
        this.dtm.addColumn("Gia Ban");
        this.dtm.addColumn("So Luong Ton");
        this.dtm.addColumn("Hang SX");
       // this.dtm.addColumn("Cua Hang");
        for (int i = 0; i < ds.size(); i++) {
            SanPham sp = ds.get(i);
            Vector vt1 = new Vector();
            vt1.add(sp.getMaSanPham());
            vt1.add(sp.getTenSanPham());
            vt1.add(sp.getGiaBan());
            vt1.add(sp.getSoLuongTon());
            HangSanXuat hsx = sp.getHangSanXuat();
            vt1.add(hsx.getTenHang());
            vt1.add(ch.getMaCuaHang());
            this.dtm.addRow(vt1);
        }
        this.jtDanhSach.setModel(dtm);
    }

    public void xoaSanPham() {
        try {
            int dong = this.jtDanhSach.getSelectedRow();
            int maSanPham = (Integer) this.jtDanhSach.getValueAt(dong, 0);
            boolean kq = SanPhamDAO.xoaSanPham(maSanPham);
            if (kq == true) {
                JOptionPane.showMessageDialog(this, "Xóa thành công");
            } else {
                JOptionPane.showMessageDialog(this, "Xóa thất bại");
            }
            //Hien thi lai danh hang san xuat
            this.hienThiDanhSach();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void capNhatSanPham(SanPham sp) {
        SanPhamDAO.capNhatThongTinSanPham(sp);
        this.hienThiDanhSach();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jbtThem = new javax.swing.JButton();
        jbtXoa = new javax.swing.JButton();
        jbtCapNhat = new javax.swing.JButton();
        jbtXoaTrang = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDanhSach = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jbtDong = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jtfMaSanPham = new javax.swing.JTextField();
        jtfSoLuongTon = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtfTenSanPham = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfGiaBan = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jcbHangSX = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jcbCuaHang = new javax.swing.JComboBox();

        setTitle("QUẢN LÝ SẢN PHẨM");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Các chức năng"));

        jbtThem.setText("Thêm");
        jbtThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThemActionPerformed(evt);
            }
        });

        jbtXoa.setText("Xóa");
        jbtXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXoaActionPerformed(evt);
            }
        });

        jbtCapNhat.setText("Cập nhật");
        jbtCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCapNhatActionPerformed(evt);
            }
        });

        jbtXoaTrang.setText("Xóa trắng");
        jbtXoaTrang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXoaTrangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtThem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jbtXoaTrang)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jbtXoa, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtThem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtXoa)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtCapNhat)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtXoaTrang)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách sản phẩm"));
        jPanel3.setToolTipText("");

        jtDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtDanhSach.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtDanhSachMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtDanhSach);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 255));
        jLabel1.setText("QUẢN LÝ SẢN PHẨM");

        jbtDong.setText("Đóng");
        jbtDong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtDongActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nhập thông tin sản phẩm"));

        jLabel2.setText("Mã sản phẩm(Auto):");

        jtfMaSanPham.setEnabled(false);

        jLabel5.setText("Giá bán:");

        jLabel6.setText("Số lượng tồn:");

        jLabel3.setText("Tên sản phẩm:");

        jLabel4.setText("Hãng SX:");

        jcbHangSX.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jtfMaSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jtfTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4))
                    .addComponent(jtfGiaBan, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfSoLuongTon)
                    .addComponent(jcbHangSX, 0, 128, Short.MAX_VALUE))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfMaSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jtfSoLuongTon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbHangSX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jtfTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfGiaBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap())
        );

        jLabel7.setText("Cửa hàng");

        jcbCuaHang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbCuaHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbCuaHangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(212, 212, 212)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(jcbCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtDong)
                        .addGap(58, 58, 58))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtDong)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThemActionPerformed
        // TODO add your handling code here:
        QuyDinh qd= new QuyDinh();
        CuaHang cuahang=(CuaHang)this.jcbCuaHang.getSelectedItem();
                
        qd=QuiDinhDAO.layAllQuyDinh(cuahang.getMaCuaHang());
        int soluong=Integer.valueOf(this.jtfSoLuongTon.getText());
        int ktraten=SanPhamDAO.KtraTenSp(this.jtfTenSanPham.getText(),cuahang.getMaCuaHang());
        if((soluong < (Integer)qd.getSoLuongNhapItNhat()) || ktraten==1){
            JOptionPane.showMessageDialog(this, "Vi Pham Quy Dinh Nhap Cua Hang", "Cảnh Báo", JOptionPane.WARNING_MESSAGE);
        }else{
        try {
            //int maSanPham = Integer.parseInt(jtfMaSanPham.getText());
            String tenSanPham = String.valueOf(jtfTenSanPham.getText());
            double giaBan = Double.parseDouble(jtfGiaBan.getText());
            int soLuongTon = Integer.parseInt(jtfSoLuongTon.getText());
            HangSanXuat hsx = (HangSanXuat) jcbHangSX.getSelectedItem();
            CuaHang ch = (CuaHang) jcbCuaHang.getSelectedItem();
            SanPham sp = new SanPham();
            sp.setTenSanPham(tenSanPham);
            sp.setGiaBan(giaBan);
            sp.setSoLuongTon(soLuongTon);
            sp.setHangSanXuat(hsx);
            sp.setCuaHang(ch);
            SanPhamDAO.themSanPham(sp);
            JOptionPane.showMessageDialog(this, "Thêm Thành Công");
            this.hienThiDanhSach();
            ///thêm vào phiếu nhập
            Date now=new Date(System.currentTimeMillis());
            PhieuNhap pn = new PhieuNhap();
            pn.setNgayNhap(now);
            PhieuNhapDAO.themPhieuNhap(pn);
            //thêm chi tiết phiếu
             pojo.ChiTietPhieuNhap ctpn = new pojo.ChiTietPhieuNhap();
            ChiTietPhieuNhapId id = new ChiTietPhieuNhapId();
            int maPhieuNhap=PhieuNhapDAO.LayMa();
            id.setMaPhieuNhap(maPhieuNhap);
            int maSanPham=SanPhamDAO.LayMa();
            id.setMaSanPham(maSanPham);
            ctpn.setId(id);
            ctpn.setSoLuongNhap(Integer.valueOf(this.jtfSoLuongTon.getText()));
            ChiTietPhieuNhapDAO.themChiTietPhieuNhap(ctpn);
            /// thêm vào báo cáo tồn
            BaoCaoTon bct = new BaoCaoTon();
             SanPham sp1 = SanPhamDAO.layThongTinSanPham(maSanPham);
            int luongTon = sp1.getSoLuongTon();
            int luonngTonNew = luongTon + Integer.valueOf(this.jtfSoLuongTon.getText());
            bct.setSanPham(sp1);
            bct.setNgayPhatSinh(now);
            bct.setTonDau(luongTon);
            bct.setPhatSinh(luonngTonNew - luongTon);
            bct.setTonCuoi(luonngTonNew);
            BaoCaoTonDAO.themThongBaoCaoTon(bct);
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex);
        }
        this.hienthidanhsachtheoch();
        }
    }//GEN-LAST:event_jbtThemActionPerformed

    private void jbtXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXoaActionPerformed
        int dong = this.jtDanhSach.getSelectedRow();
        if (dong >= 0) {
            int kq = JOptionPane.showConfirmDialog(this, "Bạn có thật sự muốn xóa?", "Thông báo", JOptionPane.YES_NO_OPTION);
            if (kq == JOptionPane.YES_OPTION) {
                this.xoaSanPham();
            }
        }
        this.hienthidanhsachtheoch();
    }//GEN-LAST:event_jbtXoaActionPerformed

    private void jbtCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCapNhatActionPerformed
        try {
            
            String tenSanPham = String.valueOf(jtfTenSanPham.getText());
            double giaBan = Double.parseDouble(jtfGiaBan.getText());
            int soLuongTon = Integer.parseInt(jtfSoLuongTon.getText());
            HangSanXuat hsx = (HangSanXuat) jcbHangSX.getSelectedItem();
            CuaHang ch = (CuaHang) jcbCuaHang.getSelectedItem();
            SanPham sp = new SanPham();
            sp.setTenSanPham(tenSanPham);
            sp.setGiaBan(giaBan);
            sp.setSoLuongTon(soLuongTon);
            sp.setHangSanXuat(hsx);
            sp.setCuaHang(ch);
            sp.setMaSanPham(Integer.valueOf(this.jtfMaSanPham.getText()));
            int kq = JOptionPane.showConfirmDialog(this, "Bạn có thật sự muốn cập nhật?", "Thông báo", JOptionPane.YES_NO_OPTION);
            if (kq == JOptionPane.YES_OPTION) {
                this.capNhatSanPham(sp);
            }
            JOptionPane.showMessageDialog(this, "Cập Nhật Thành Công");
            this.hienthidanhsachtheoch();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex);
        }
    }//GEN-LAST:event_jbtCapNhatActionPerformed

    private void jbtXoaTrangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXoaTrangActionPerformed
        this.jtfMaSanPham.setText("");
        this.jtfTenSanPham.setText("");
        this.jtfGiaBan.setText("");
        this.jtfSoLuongTon.setText("");
    }//GEN-LAST:event_jbtXoaTrangActionPerformed

    private void jtDanhSachMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtDanhSachMouseClicked
        // TODO add your handling code here:
        int dong = jtDanhSach.getSelectedRow();
        int maSanPham = (Integer) this.jtDanhSach.getValueAt(dong, 0);
        String tenSanPham = (String) this.jtDanhSach.getValueAt(dong, 1);
        double giaBan = (double) this.jtDanhSach.getValueAt(dong, 2);
        int soLuongTon = (Integer) this.jtDanhSach.getValueAt(dong, 3);
//        int maHangSX = (Integer) this.jtDanhSach.getValueAt(dong, 4);
//        int maCuaHang = (Integer) this.jtDanhSach.getValueAt(dong, 5);
//        HangSanXuat hsx = HangSanXuatDAO.layThongTinNhaSanXuat(maHangSX);
//        CuaHang ch = CuaHangDAO.layThongTinCuaHang(maCuaHang);
        this.jtfMaSanPham.setText(String.valueOf(maSanPham));
        this.jtfTenSanPham.setText(tenSanPham);
        this.jtfGiaBan.setText(String.valueOf(giaBan));
        this.jtfSoLuongTon.setText(String.valueOf(soLuongTon));
//        this.jcbCuaHang.setSelectedItem(ch);
//        this.jcbHangSX.setSelectedItem(hsx);
    }//GEN-LAST:event_jtDanhSachMouseClicked

    private void jbtDongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtDongActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtDongActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        try {
            List<HangSanXuat> ds = HangSanXuatDAO.layDanhSachHangSanXuat();
            for (int i = 0; i < ds.size(); i++) {
                HangSanXuat hsx = ds.get(i);
                dcbm.addElement(hsx);
            }
            List<CuaHang> ds1 = CuaHangDAO.layDanhSachCuaHang();
            if(this.user.getPhanQuyen()==2){
                int mach=this.user.getCuaHang().getMaCuaHang();
                for (int i = 0; i < ds1.size(); i++) {
                    CuaHang ch = ds1.get(i);
                    if(ch.getMaCuaHang()==mach){
                    dcbm1.addElement(ch);
                    }
                    }
            }else{
                for (int i = 0; i < ds1.size(); i++) {
                    CuaHang ch = ds1.get(i);
                    dcbm1.addElement(ch);
                    }
            }
           // this.hienThiDanhSach();
            this.hienthidanhsachtheoch();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_formInternalFrameOpened

    private void jcbCuaHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbCuaHangActionPerformed
        // TODO add your handling code here:
        this.hienthidanhsachtheoch();
    }//GEN-LAST:event_jcbCuaHangActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtCapNhat;
    private javax.swing.JButton jbtDong;
    private javax.swing.JButton jbtThem;
    private javax.swing.JButton jbtXoa;
    private javax.swing.JButton jbtXoaTrang;
    private javax.swing.JComboBox jcbCuaHang;
    private javax.swing.JComboBox jcbHangSX;
    private javax.swing.JTable jtDanhSach;
    private javax.swing.JTextField jtfGiaBan;
    private javax.swing.JTextField jtfMaSanPham;
    private javax.swing.JTextField jtfSoLuongTon;
    private javax.swing.JTextField jtfTenSanPham;
    // End of variables declaration//GEN-END:variables
}
